
export interface Result {
    //统一返回自定义状态码处理
    status: number;
    data: any[];
    page: any
}
const getModuleList = (intance, data) => {
    const list = ['id', 'buildings_name', 'floor_name', 'department_name', 'unit_name', 'name']
    const { $axios } = intance;

    let item: Result = {
        data: [],
        page: {},
        status: 0,
    };

    return new Promise((resolve, reject) => {
        $axios({
            url: `api/teacher/officeArrange/getList?page=${data.page}&limit=${data.limit}`,
            method: 'get',
        }).then(res => {
            if (res && res.status === 200) {
                item.data = res.data.data.map(el => {
                    let obj = Object.assign({}, el)
                    Object.entries(obj).forEach((el, i) => {
                        if (!list.includes(el[0])) delete obj[el[0]]
                    })
                    return obj
                })
                item.page = {
                    total: res.data.count,
                    current_page: res.data.page,
                    per_page: res.data.limit
                }
                resolve(item);
            } else {
                item.status = 1;
                resolve(item);
            }
        })
    })
}

const getBuildingList = (intance, data) => {
    const { $axios } = intance;

    let item: Result = {
        data: [],
        page: {},
        status: 0,
    };

    return new Promise((resolve, reject) => {
        $axios({
            url: `api/material/buildings/getNoPageList`,
            method: 'get',
        }).then(res => {
            if (res && res.status === 200) {
                item.data = res.data.data
                delete item.page
                resolve(item);
            } else {
                item.status = 1;
                resolve(item);
            }
        })
    })
}

const getUnitList = (intance, data) => {
    const { $axios } = intance;

    let item: Result = {
        data: [],
        page: {},
        status: 0,
    };

    return new Promise((resolve, reject) => {
        $axios({
            url: `api/material/unit/getNoPageList?buildings_id=${data}`,
            method: 'get',
        }).then(res => {
            if (res && res.status === 200) {
                item.data = res.data.data
                delete item.page
                resolve(item);
            } else {
                item.status = 1;
                resolve(item);
            }
        })
    })
}

const getFloorList = (intance, data) => {
    const { $axios } = intance;

    let item: Result = {
        data: [],
        page: {},
        status: 0,
    };

    return new Promise((resolve, reject) => {
        $axios({
            url: `api/material/floor/getNoPageList?unit_id=${data}`,
            method: 'get',
        }).then(res => {
            if (res && res.status === 200) {
                item.data = res.data.data
                delete item.page
                resolve(item);
            } else {
                item.status = 1;
                resolve(item);
            }
        })
    })
}

const getRoomList = (intance, data) => {
    const { $axios } = intance;

    let item: Result = {
        data: [],
        page: {},
        status: 0,
    };

    return new Promise((resolve, reject) => {
        $axios({
            url: `api/material/room/getNoPageList?floor_id=${data}`,
            method: 'get',
        }).then(res => {
            if (res && res.status === 200) {
                item.data = res.data.data
                delete item.page
                resolve(item);
            } else {
                item.status = 1;
                resolve(item);
            }
        })
    })
}

const postData = (intance, data) => {
    const { $axios } = intance;

    let item: Result = {
        data: [],
        page: {},
        status: 0,
    };
    return new Promise((resolve, reject) => {
        $axios({
            url: `api/teacher/officeArrange/save`,
            data,
            method: 'post',
        }).then(res => {
            if (res && res.status === 200) {
                item.data = res.data.data
                delete item.page
                resolve(item);
            } else {
                item.status = 1;
                resolve(item);
            }
        })
    })
}

const putData = (intance, data) => {
    const { $axios } = intance;

    let item: Result = {
        data: [],
        page: {},
        status: 0,
    };
    return new Promise((resolve, reject) => {
        $axios({
            url: `api/teacher/officeArrange/update`,
            data,
            method: 'put',
        }).then(res => {
            if (res && res.status === 200) {
                item.data = res.data.data
                delete item.page
                resolve(item);
            } else {
                item.status = 1;
                resolve(item);
            }
        })
    })
}

const getDepartmentList = (intance, data) => {
    const { $axios } = intance;

    let item: Result = {
        data: [],
        page: {},
        status: 0,
    };

    return new Promise((resolve, reject) => {
        $axios({
            url: `api/systemctl/orgDepartments/getNoPageList`,
            method: 'get',
        }).then(res => {
            if (res && res.status === 200) {
                item.data = res.data.data
                delete item.page
                resolve(item);
            } else {
                item.status = 1;
                resolve(item);
            }
        })
    })
}

const getData = (intance, data) => {
    const { $axios } = intance;

    let item: Result = {
        data: [],
        page: {},
        status: 0,
    };

    return new Promise((resolve, reject) => {
        $axios({
            url: `api/teacher/officeArrange/getById/${data}`,
            method: 'get',
        }).then(res => {
            if (res && res.status === 200) {
                item.data = res.data.data
                delete item.page
                resolve(item);
            } else {
                item.status = 1;
                resolve(item);
            }
        })
    })
}

const deleteData = (intance, data) => {
    const { $axios } = intance;

    let item: Result = {
        data: [],
        page: {},
        status: 0,
    };

    return new Promise((resolve, reject) => {
        $axios({
            url: `api/teacher/officeArrange/delById/${data}`,
            method: 'delete',
        }).then(res => {
            if (res && res.status === 200) {
                delete item.page
                resolve(item);
            } else {
                item.status = 1;
                resolve(item);
            }
        })
    })
}

export { getModuleList, getBuildingList, getUnitList, getFloorList, getRoomList, postData, getDepartmentList, getData, putData, deleteData }
